home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 2 / CU Amiga Magazine's Super CD-ROM 02 (1996)(EMAP Images)(GB)[!][issue 1996-04].iso / magazine / amiga_e / scaniff.e < prev    next >
Text File  |  1995-01-11  |  3KB  |  150 lines

  1. /****
  2.  
  3.     ScanIFF.e
  4.     © 1995 by Vidar Hokstad <vidarh@rforum.no>
  5.  
  6.  
  7.     COPYRIGHT NOTICE:
  8.  
  9.     This code can be distributed freely, and parts of the code,
  10.     or the whole code, can be used as is, or reused in any
  11.     product - free or commercial - provided the following terms
  12.     are met:
  13.  
  14.     - You accept that I give no guarantee, expressed or implied
  15.         of the usefulness or functionality of this code, and that
  16.         I accept no responsability for damage caused directly or
  17.         indirectly by the use of this program.
  18.  
  19.     - The product in which the code is used can not be used
  20.         for military applications.
  21.  
  22.  
  23.     INFO:
  24.  
  25.     Written as an exersize in using iffparse.library. It reads an
  26.     IFF file, and dumps the contents of any occurences of a given
  27.     type of chunk, given the type and id.
  28.  
  29. ***/
  30.  
  31.  
  32. OPT OSVERSION=37
  33.  
  34. MODULE 'iffparse','libraries/iffparse','dos/dos'
  35.  
  36. RAISE "^C" IF CtrlC() = TRUE
  37.  
  38. -> Convert type/id string to a longword
  39.  
  40. PROC gettype (str)
  41.     DEF ret
  42.     MOVE.L    str,A0
  43.     MOVE.B    (A0)+,D0
  44.     ASL.W    #8,D0
  45.     MOVE.B    (A0)+,D0
  46.     SWAP    D0
  47.     MOVE.B    (A0)+,D0
  48.     ASL.W    #8,D0
  49.     MOVE.B    (A0),D0
  50.     MOVE.L    D0,ret
  51. ENDPROC ret
  52.  
  53. PROC scaniff (name,type,id) HANDLE
  54.  
  55.     DEF iff:PTR TO iffhandle,        -> Utility struct. for iffparse
  56.         node:PTR TO contextnode,    -> additional info about chunk
  57.         fh,                            -> File handle of an IFF file
  58.         buf[16]:ARRAY OF CHAR,        -> Buffer for ReadChunkBytes()
  59.         len,i
  60.  
  61. -> --- INIT
  62.  
  63.     IF (iffparsebase:= OpenLibrary('iffparse.library',0))=NIL
  64.         Raise ("iffp")
  65.     ENDIF
  66.  
  67.     -> You *MUST* use AllocIFF() to allocate an iffhandle structure
  68.     IF (iff:=AllocIFF())=NIL THEN Raise ("iffh")
  69.  
  70.     -> Prepare the iffhandle to use a dos filehandle
  71.     InitIFFasDOS(iff)
  72.  
  73.     -> Open a file and fill inn the iffhandle
  74.     IF (fh:=Open (name,OLDFILE) )=0 THEN Raise ("open")
  75.     iff.stream:=fh
  76.  
  77.     -> Start a new IO session
  78.     IF OpenIFF (iff,IFFF_READ) THEN Raise ("oiff")
  79.  
  80.     IF StopChunk (iff,gettype(type),gettype(id)) THEN Raise ("schn")
  81.  
  82.  
  83. -> --- MAIN LOOP
  84.  
  85. ->         While there's something left to read, parse the IFF stream.
  86.  
  87.     WHILE ParseIFF(iff,IFFPARSE_SCAN)<>IFFERR_EOF
  88.         CtrlC()
  89.  
  90.         node:= CurrentChunk(iff)
  91.         Vprintf ('"%s" / "%s", size = %ld {\n',
  92.                 [[node.id,0],[node.type,0],node.size])
  93.  
  94.         -> Dump the contents of the requested chunk
  95.         WHILE (len:=ReadChunkBytes(iff,buf,16))>0
  96.             PutStr ('  ')
  97.             FOR i:=0 TO len-1 DO Vprintf (' $%02.lx',[buf[i]])
  98.             PutStr ('\n')
  99.         ENDWHILE
  100.  
  101.         PutStr ('}\n')
  102.     ENDWHILE
  103.  
  104.     PutStr ('\n\n')
  105. EXCEPT DO
  106.  
  107. ->--- CLEANUP:
  108.  
  109.     -> Was iffparse.library opened?
  110.     IF iffparsebase
  111.  
  112.         -> Was the iffhandle structure allocated?
  113.         IF iff
  114.  
  115.             -> Did OpenIFF() fail?
  116.             IF exception<>"oiff" THEN CloseIFF(iff)
  117.  
  118.             -> Was the file opened?
  119.             IF fh THEN Close(fh)
  120.  
  121.             -> Free the iffhandle. *MUST* be done with FreeIFF()
  122.             FreeIFF(iff)
  123.         ENDIF
  124.         CloseLibrary (iffparsebase)
  125.     ENDIF
  126.  
  127.     -> IF an exception occured, let the next exception handler deal
  128.     -> with it too...
  129.  
  130.     ReThrow()
  131. ENDPROC
  132.  
  133. PROC main() HANDLE
  134.     DEF rdargs,args
  135.  
  136.     args:=[0,0,0]
  137.     IF rdargs:=ReadArgs ('FILENAME/A,TYPE/A,ID/A',args,NIL)
  138.         scaniff (args[0],args[1],args[2])
  139.     ELSE
  140.         PrintFault (ERROR_BAD_TEMPLATE,'Dos error')
  141.     ENDIF
  142. EXCEPT
  143.     IF exception = "^C"
  144.         PutStr ('***BREAK\n')
  145.     ELSE
  146.         Vprintf ('exception = %ld ("%s")\n',[exception,[exception,0]])
  147.     ENDIF
  148. ENDPROC
  149.  
  150.